import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpService } from '../shared/http/http.service';
import { ToastService } from '../shared/toast/toast.service';
import { ToastConfig, ToastType } from '../shared/toast/toast-model';
import { PassportService } from '../services/passport.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html'
})
export class LoginComponent implements OnInit {

  constructor(
      private router: Router,
      private toastService: ToastService,
      private httpService: HttpService,
      private passportService: PassportService
  ) { }

  userForm = {
      userName: '',
      password: ''
  };
   /**
   * 初始化
   */
  ngOnInit() {

  }

  /**
   * 登录
   */
  login() {
    const that = this;
    console.log(this.userForm.userName);

    this.passportService.logout();
    this.passportService.login(this.userForm, function(successful, profile){
        console.log(profile);
        if (successful && profile) {
            that.router.navigate(['/app/home']);
        }else {
            const toastCfg = new ToastConfig(ToastType.ERROR, '', '登录失败,请稍候重试', 3000);
            that.toastService.toast(toastCfg);
        }
    });
  }
}
